package objects.alignments;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import objects.ValidPosition;

import Control.Zoomable;

public class SpliceCollapsedAlignment extends CollapsedAlignment implements
		Subset0Alignment {

	public SpliceCollapsedAlignment(Subset0Alignment alignment) {
		super(alignment);
	}
	
	public SpliceCollapsedAlignment(SpliceAlignment one, SpliceAlignment two) {
		super(one);
		this.addAlignment(two);
	}

	@Override
	public void updateReferencePosition() {
		if (getAligmentCount() == 0)
			return;

		referencePosition = containedAlignments.get(0).getValidPositions()
				.get(0).end;
	}

	@Override
	public boolean isCandidateAddition(Subset0Alignment temp) {
		if (temp instanceof SpliceCollapsedAlignment) {
			int spliceStart = ((SpliceCollapsedAlignment) temp).containedAlignments
					.get(0).getValidPositions().get(0).end;
			int spliceEnd = ((SpliceCollapsedAlignment) temp).containedAlignments
					.get(0).getValidPositions().get(1).start;

			int end = containedAlignments.get(0).getValidPositions().get(1).start;

			return ((referencePosition - spliceStart <= COLLAPSE_WIGGLE) && (end - spliceEnd <= COLLAPSE_WIGGLE));
		}
		return false;
	}

	@Override
	public void draw(Graphics graphic, int x, int y) {
		// TODO Auto-generated method stub

		Color color = new Color(255, 255, 255);
		Graphics2D g2d = (Graphics2D) graphic;
		Zoomable z = Zoomable.getInstance();

		// Get the Splice Site
		int spliceStart = containedAlignments.get(0).getValidPositions().get(0).end;
		int spliceEnd = containedAlignments.get(0).getValidPositions().get(1).start;

		ValidPosition vp = this.getMaxBound();

		g2d.setColor(color);
		ValidPosition firstVP = new ValidPosition(vp.start, spliceStart);
		ValidPosition seconVP = new ValidPosition(spliceEnd, vp.end);
		firstVP.x = x + z.calcDist(firstVP.start);
		firstVP.y = y - 1;
		firstVP.width = z.calcDist(firstVP.getDistance());
		firstVP.height = 6;
		g2d.fill(firstVP);

		seconVP.x = x + z.calcDist(seconVP.start);
		seconVP.y = y - 1;
		seconVP.width = z.calcDist(seconVP.getDistance());
		seconVP.height = 6;
		g2d.fill(seconVP);

		g2d.setColor(new Color(0, 0, 0));
		g2d.draw(firstVP);
		g2d.draw(seconVP);

		g2d.fillRect((int) Math.round(firstVP.x + firstVP.width), y + 2,
				(int) Math.round(seconVP.x - (firstVP.x + firstVP.width)), 2);
	}

	@Override
	public String toString() {
		return "Splice Collapsed Alignment";
	}

}
